#include "ElectionRegistry.h"

ElectionRegistry::ElectionRegistry() : creators() {};

ElectionRegistry::~ElectionRegistry() {
    map<string,ElectionCreator*>::iterator i = creators.begin();
    while( i != creators.end() ) {
        delete i->second;
        ++i;
    }
    creators.erase( creators.begin(), creators.end() );
}

int ElectionRegistry::add( string name, ElectionCreator* ec ) {
    creators[name] = ec;
    return 1;
}

int ElectionRegistry::size() const {
    return creators.size();
}

ElectionBase* ElectionRegistry::create( string type, set<string> candidates ) const {
    map<string,ElectionCreator*>::const_iterator ec = creators.find( type );
    if( ec == creators.end() ) return 0;
    return ec->second->create( candidates ); 
}

map<string,ElectionCreator*> ElectionRegistry::get_creators() const {
    return creators;
}

set<string> ElectionRegistry::get_names() const {
    set<string> keys;
    map<string,ElectionCreator*>::const_iterator m = creators.begin();
    while( m != creators.end() ) {
        keys.insert( m->first );
        ++m;
    }
    return keys;
}
